function [isCorrect] = Point_Inside_Environment(X,Y,Z,i)
format short;

if (i == 1)
    
    
%p1p2p
    x0 = Z(1,2); y0 = Z(1,3); x1 = Z(1,4); y1 = Z(1,5); x2 = X; y2 = Y;
    a1 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p2p3p
    x0 = Z(1,4); y0 = Z(1,5); x1 = Z(1,6); y1 = Z(1,7); x2 = X; y2 = Y;
    a2 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p3p4p
    x0 = Z(1,6); y0 = Z(1,7); x1 = Z(1,8); y1 = Z(1,9); x2 = X; y2 = Y;
    a3 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p4p1p
    x0 = Z(1,8); y0 = Z(1,9); x1 = Z(1,2); y1 = Z(1,3); x2 = X; y2 = Y;
    a4 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);   
    
    
end

if (i == 2)
    
%p1p2p
    x0 = Z(2,2); y0 = Z(2,3); x1 = Z(2,4); y1 = Z(2,5); x2 = X; y2 = Y;
    a1 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p2p3p
    x0 = Z(2,4); y0 = Z(2,5); x1 = Z(2,6); y1 = Z(2,7); x2 = X; y2 = Y;
    a2 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p3p4p
    x0 = Z(2,6); y0 = Z(2,7); x1 = Z(2,8); y1 = Z(2,9); x2 = X; y2 = Y;
    a3 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p4p1p
    x0 = Z(2,8); y0 = Z(2,9); x1 = Z(2,2); y1 = Z(2,3); x2 = X; y2 = Y;
    a4 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);   
    
    
    
end

if (i == 3)
    
%p1p2p
    x0 = Z(3,2); y0 = Z(3,3); x1 = Z(3,4); y1 = Z(3,5); x2 = X; y2 = Y;
    a1 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p2p3p
    x0 = Z(3,4); y0 = Z(3,5); x1 = Z(3,6); y1 = Z(3,7); x2 = X; y2 = Y;
    a2 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p3p4p
    x0 = Z(3,6); y0 = Z(3,7); x1 = Z(3,8); y1 = Z(3,9); x2 = X; y2 = Y;
    a3 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p4p1p
    x0 = Z(3,8); y0 = Z(3,9); x1 = Z(3,2); y1 = Z(3,3); x2 = X; y2 = Y;
    a4 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);      
    
    
end

if (i == 4)
    
%p1p2p
    x0 = Z(4,2); y0 = Z(4,3); x1 = Z(4,4); y1 = Z(4,5); x2 = X; y2 = Y;
    a1 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p2p3p
    x0 = Z(4,4); y0 = Z(4,5); x1 = Z(4,6); y1 = Z(4,7); x2 = X; y2 = Y;
    a2 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p3p4p
    x0 = Z(4,6); y0 = Z(4,7); x1 = Z(4,8); y1 = Z(4,9); x2 = X; y2 = Y;
    a3 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p4p1p
    x0 = Z(4,8); y0 = Z(4,9); x1 = Z(4,2); y1 = Z(4,3); x2 = X; y2 = Y;
    a4 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);   
       
end



if (i == 5)
    
%p1p2p
    x0 = Z(5,2); y0 = Z(5,3); x1 = Z(5,4); y1 = Z(5,5); x2 = X; y2 = Y;
    a1 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p2p3p
    x0 = Z(5,4); y0 = Z(5,5); x1 = Z(5,6); y1 = Z(5,7); x2 = X; y2 = Y;
    a2 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p3p4p
    x0 = Z(5,6); y0 = Z(5,7); x1 = Z(5,8); y1 = Z(5,9); x2 = X; y2 = Y;
    a3 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p4p1p
    x0 = Z(5,8); y0 = Z(5,9); x1 = Z(5,2); y1 = Z(5,3); x2 = X; y2 = Y;
    a4 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);   
       
end



if (i == 6)
    
%p1p2p
    x0 = Z(6,2); y0 = Z(6,3); x1 = Z(6,4); y1 = Z(6,5); x2 = X; y2 = Y;
    a1 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p2p3p
    x0 = Z(6,4); y0 = Z(6,5); x1 = Z(6,6); y1 = Z(6,7); x2 = X; y2 = Y;
    a2 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p3p4p
    x0 = Z(6,6); y0 = Z(6,7); x1 = Z(6,8); y1 = Z(6,9); x2 = X; y2 = Y;
    a3 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p4p1p
    x0 = Z(6,8); y0 = Z(6,9); x1 = Z(6,2); y1 = Z(6,3); x2 = X; y2 = Y;
    a4 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);   
       
end



if (i == 7)
    
%p1p2p
    x0 = Z(7,2); y0 = Z(7,3); x1 = Z(7,4); y1 = Z(7,5); x2 = X; y2 = Y;
    a1 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p2p3p
    x0 = Z(7,4); y0 = Z(7,5); x1 = Z(7,6); y1 = Z(7,7); x2 = X; y2 = Y;
    a2 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p3p4p
    x0 = Z(7,6); y0 = Z(7,7); x1 = Z(7,8); y1 = Z(7,8); x2 = X; y2 = Y;
    a3 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p4p1p
    x0 = Z(7,8); y0 = Z(7,9); x1 = Z(7,2); y1 = Z(7,3); x2 = X; y2 = Y;
    a4 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);   
       
end



if (i == 8)
    
%p1p2p
    x0 = Z(8,2); y0 = Z(8,3); x1 = Z(8,4); y1 = Z(8,5); x2 = X; y2 = Y;
    a1 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p2p3p
    x0 = Z(8,4); y0 = Z(8,5); x1 = Z(8,6); y1 = Z(8,7); x2 = X; y2 = Y;
    a2 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p3p4p
    x0 = Z(8,6); y0 = Z(8,7); x1 = Z(8,8); y1 = Z(8,9); x2 = X; y2 = Y;
    a3 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p4p1p
    x0 = Z(8,8); y0 = Z(8,9); x1 = Z(8,2); y1 = Z(8,3); x2 = X; y2 = Y;
    a4 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);   
       
end



if (i == 9)
    
%p1p2p
    x0 = Z(9,2); y0 = Z(9,3); x1 = Z(9,4); y1 = Z(9,5); x2 = X; y2 = Y;
    a1 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p2p3p
    x0 = Z(9,4); y0 = Z(9,5); x1 = Z(9,6); y1 = Z(9,7); x2 = X; y2 = Y;
    a2 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p3p4p
    x0 = Z(9,6); y0 = Z(9,7); x1 = Z(9,8); y1 = Z(9,9); x2 = X; y2 = Y;
    a3 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);

%p4p1p
    x0 = Z(9,8); y0 = Z(9,9); x1 = Z(9,2); y1 = Z(9,3); x2 = X; y2 = Y;
    a4 = (0.5) * (x1 * y2 - y1 * x2 - x0 * y2 + y0 * x2 + x0 * y1 - y0 * x1);   
       
end


if ((a1 > 0) && (a2 > 0) && (a3 > 0) && (a4 > 0)) || ((a1 < 0) && (a2 < 0) && (a3 < 0) && (a4 < 0))
    isCorrect = 1;
else
    isCorrect = 0;
end

end